<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 歌曲管理
 *
 * @icon fa fa-circle-o
 */
class Song extends Backend
{

    /**
     * Song模型对象
     * @var \app\admin\model\Song
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Song;

    }


    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
        $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
        foreach ($list as $k => &$v) {
            $v['fullurl'] = (strstr($v['file'], 'http') == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['file'];
        }
        
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


}
